import React, { useState } from 'react'
import { Row, Col, Radio, InputNumber, Slider, Button, Checkbox, Input, Space, Popover, Dropdown, Tag, Tooltip, Switch } from 'antd'
import EditLink from '@/components/EditLink';
import UploadImage from '@/components/UploadImage';
import SelectRadioModel from '@/components/SelectRadioModel'
import UploadSelectImage from '../../../UploadSelectImage';
import { backgroundObj } from '@/utils/utils'
import { CloseOutlined } from '@ant-design/icons'
import { SketchPicker } from 'react-color';
import SelectLink from '@/components/SelectLink';
import './index.less'
const DisplayAdvertising = (props) => {
    const { selectData, data, edit } = props
    const { selectIndex } = data
    const { list, marginType, top, bottom, backgroundType, backgroundColor, countType, pictureTitleShow, paddingLeft, paddingTop, radiusType, borderRadius, backgroundImage } = selectData
    const [visible, setVisible] = useState(false)
    const [editVisible, setEditVisible] = useState(false)
    const [shopEdit, setShopEdit] = useState(false)
    const [checkVisible, setCheckVisible] = useState(false)
    const [bgVisible, setBgVisible] = useState(false)
    const swiperListProps = {
        title: "",
        src: "",
        linkType: "",
        linkData: {},
        linkSrc: '',
        showIphone: false
    }
    const handleUpdate = (o) => {
        edit({
            list: data.list.map((val, index) => {
                if (index === selectIndex) {
                    return {
                        ...val,
                        ...o
                    }
                }
                return val
            })
        })
    }
    const renderType = {
        one: '一行一张',
        two: "一行两张",
        three: "一行三张",
        swiper: "轮播图",
        bigImage: "大图横向滑动",
        smallImage: "小图横向滑动",
    }[countType]
    return <Row gutter={[12, 24]}>
        <Col
            span={24}>
            图片广告
        </Col>
        <Col className='editor-content-item-label'
            span={24}>
            组件模版  <span style={{ color: '#333333' }}> {renderType}</span>
        </Col>
        <Col className='editor-content-item-label'
            span={24}>
            <Radio.Group onChange={(e) => {
                handleUpdate({
                    countType: e.target.value
                })
            }} value={countType}>
                <Radio.Button value={'one'}>一张</Radio.Button>
                <Radio.Button value={'two'}>两张</Radio.Button>
                <Radio.Button value={'three'}>三张</Radio.Button>
                <Radio.Button value={'swiper'}>轮播图</Radio.Button>
                <Radio.Button value={'bigImage'}>大图横向滑动</Radio.Button>
                <Radio.Button value={'smallImage'}>小图横向滑动</Radio.Button>
            </Radio.Group>
        </Col>
        <Col className='editor-content-item-label'
            span={24}>
            添加内容
        </Col>
        <Col className='editor-content-item-label'
            span={24}>
            建议图片宽度 340px，大小不超过 5M
        </Col>
        <Col className='editor-swiper-setting'
            span={24}>
            {list.map((val, index) => {
                const {
                    src,
                    title,
                    showIphone,
                    linkType
                } = val
                return <div key={index} className='imageAd-item'>
                    <div onClick={() => {
                        setVisible({
                            show: true,
                            index: index,
                            type: "edit"
                        })
                    }} className='imageAd-item__image'>
                        <img src={src} style={{ width: '100%', height: "100%" }} />
                        <div className='imageAd-item__imageUpload'>修改</div>
                    </div>
                    <Space style={{ width: "100%" }} direction='vertical'>
                        <div>
                            <div>标题 <Input
                                onChange={(e) => {
                                    handleUpdate({
                                        list: list.map((item, o) => {
                                            if (index === o) {
                                                return {
                                                    ...item,
                                                    title: e.target.value
                                                }
                                            }
                                            else {
                                                return item
                                            }
                                        })
                                    })
                                }}
                                value={title}></Input></div>
                        </div>
                        <div>
                            <Space>
                                <div>
                                    跳转链接:
                                </div>
                                <SelectLink handleUpdate={(e) => {
                                    handleUpdate({
                                        list: list.map((item, o) => {
                                            if (index === o) {
                                                return {
                                                    ...item,
                                                    ...e
                                                }
                                            }
                                            else {
                                                return item
                                            }
                                        })
                                    })
                                }} data={val}></SelectLink>
                            </Space>

                        </div>
                        <div>
                            <Checkbox onChange={(e) => {
                                console.log(e)
                                handleUpdate({
                                    list: list.map((item, o) => {
                                        if (index === o) {
                                            return {
                                                ...item,
                                                showIphone: e.target.checked
                                            }
                                        }
                                        else {
                                            return item
                                        }
                                    })
                                })
                            }} checked={showIphone}>在苹果小程序中隐藏</Checkbox>
                        </div>
                        <CloseOutlined onClick={(e) => {
                            e.preventDefault();
                            handleUpdate({
                                list: list.filter((item, o) => {
                                    return o !== index
                                })
                            })
                        }} className='close_icon'></CloseOutlined>
                    </Space>
                </div>
            })}

            <div onClick={() => {
                setCheckVisible({
                    show: true
                })
            }} className='imageAd-add-btn'>
                <Button type='link'>+添加图片</Button>
            </div>
            {/* <div className='upload-text'>建议尺寸750x280像素，图片大小不超过2M</div>
            <div className='upload-text'>最多添加12个轮播图，拖动选中的轮播图可对其排序</div> */}
        </Col>
        <Col className='editor-content-item-label'
            span={24}>
            内容展示
        </Col>
        <Col className='editor-content-item-label'
            span={24}>
            <Space>
                <div>图片标题</div>
                <div style={{ color: '#333333' }}>显示</div>
                <div><Switch onChange={(e) => {
                    handleUpdate({
                        pictureTitleShow: e
                    })
                }} checked={pictureTitleShow}></Switch></div>
            </Space>
        </Col>
        <Col className='editor-content-item-label'
            span={24}>
            边角样式:
            <Radio.Group onChange={(e) => {
                handleUpdate({
                    radiusType: e.target.value
                })
            }} value={radiusType}>
                <Radio value={'rightAngle'}>直角</Radio>
                <Radio value={'roundedCorners'}>圆角</Radio>
            </Radio.Group>
        </Col>
        {radiusType === 'roundedCorners' && <Col
            span={24}>
            <Row gutter={[12, 12]}>
                <Col span={6}>圆角半径</Col>
                <Col span={12}>
                    <Slider
                        min={0}
                        max={16}
                        onChange={(e) => handleUpdate({ borderRadius: e })}
                        value={borderRadius}
                    />
                </Col>
                <Col span={6}><InputNumber min={0}
                    max={16} onChange={(e) => {
                        handleUpdate({ borderRadius: e })
                    }} value={borderRadius}></InputNumber></Col>
            </Row>
        </Col>}
        <Col className='editor-content-item-label'
            span={24}>
            组件样式
        </Col>
        <Col
            span={24}>
            <Row gutter={[12, 12]}>
                <Col span={6}>左右内边距</Col>
                <Col span={12}>
                    <Slider
                        min={0}
                        max={32}
                        onChange={(e) => handleUpdate({ paddingLeft: e })}
                        value={paddingLeft}
                    />
                </Col>
                <Col span={6}><InputNumber min={0}
                    max={32} onChange={(e) => {
                        handleUpdate({ paddingLeft: e })
                    }} value={paddingLeft}></InputNumber></Col>
            </Row>
        </Col>
        <Col
            span={24}>
            <Row gutter={[12, 12]}>
                <Col span={6}>上下内边距</Col>
                <Col span={12}>
                    <Slider
                        min={0}
                        max={32}
                        onChange={(e) => handleUpdate({ paddingTop: e })}
                        value={paddingTop}
                    />
                </Col>
                <Col span={6}><InputNumber min={0}
                    max={32} onChange={(e) => {
                        handleUpdate({ paddingTop: e })
                    }} value={paddingTop}></InputNumber></Col>
            </Row>
        </Col>
        <Col className='editor-content-item-label'
            span={24}>
            外边距：
            <Radio.Group onChange={(e) => {
                handleUpdate({
                    top: 12,
                    bottom: 0,
                    marginType: e.target.value
                })
            }} value={marginType}>
                <Radio value={'detault'}>默认</Radio>
                <Radio value={'customize'}>自定义</Radio>
            </Radio.Group>
        </Col>
        {marginType === 'customize' && <Col
            span={24}>
            <Row gutter={[12, 12]}>
                <Col span={6}>上</Col>
                <Col span={12}>
                    <Slider
                        min={0}
                        max={32}
                        onChange={(e) => handleUpdate({ top: e })}
                        value={top}
                    />
                </Col>
                <Col span={6}><InputNumber min={0}
                    max={32} onChange={(e) => {
                        handleUpdate({ top: e })
                    }} value={top}></InputNumber></Col>
            </Row>
            <Row gutter={[12, 12]}>
                <Col span={6}>下</Col>
                <Col span={12}>
                    <Slider
                        min={0}
                        max={32}
                        onChange={(e) => handleUpdate({ bottom: e })}
                        value={bottom}
                    />
                </Col>
                <Col span={6}><InputNumber min={0}
                    max={32} onChange={(e) => {
                        handleUpdate({ bottom: e })
                    }} value={bottom}></InputNumber></Col>
            </Row>
        </Col>}
        <Col className='editor-content-item-label'
            span={24}>
            组件背景
        </Col>
        <Col span={24}>
            <Radio.Group onChange={(e) => {
                handleUpdate({ backgroundType: e.target.value, backgroundColor: "#ffffff", backgroundImage: '' })
            }} value={backgroundType}>
                <Radio value={'default'}>默认</Radio>
                <Radio value={'color'}>颜色</Radio>
                <Radio value={'image'}>图片</Radio>
            </Radio.Group>
        </Col>
        {backgroundType === 'color' ? <Col span={24}>
            <div className='editor-set-color'>
                <div className='design-editor-content-item-label'>
                    自定义颜色
                </div>
                <div className='design-editor-link'>
                    <Space>
                        {/* <SketchPicker color={backgroundColor}
                            onChangeComplete={(e) => {
                                edit({
                                    backgroundColor: e
                                })

                            }}></SketchPicker> */}
                        <Button type='link'>重置</Button>
                        <Popover content={<SketchPicker
                            color={backgroundColor}
                            onChange={(e) => {
                                handleUpdate({
                                    backgroundColor: e.hex
                                })

                            }}></SketchPicker>} trigger={['click']} placement={'bottom'}>
                            <div className='design-color-set'>
                                <div style={{ background: backgroundColor }} className='design-color-block '></div>
                            </div>
                        </Popover>

                    </Space>
                </div>
            </div>
        </Col> : backgroundType === 'image' ? <Col span={24}>
            <div className='editor-set-color' onClick={(e) => {
                setBgVisible({
                    show: true,
                })
            }}>
                {backgroundImage ? <div style={backgroundObj(backgroundImage)} className='editor-set-uploadBlock'>
                </div> : <div className='editor-set-uploadBlock'>
                    <div className='editor-set-upload'>
                        <i className='icon-jia'></i>
                    </div>
                </div>}
            </div>
        </Col> : ""}
        <UploadSelectImage handleImage={(e) => {
            if (e && e.length) {
                handleUpdate({
                    list: [...list, ...e.map(val => {
                        return {
                            ...swiperListProps,
                            src: val.src
                        }
                    })]
                })
            }
        }} visible={checkVisible} onClose={() => { setCheckVisible(false) }}></UploadSelectImage>
        <UploadImage handleImage={(e) => {
            if (e) {
                const { src } = e
                const { type, index } = visible
                if (type === 'edit') {
                    handleUpdate({
                        list: list.map((item, o) => {
                            if (index === o) {
                                return {
                                    ...item,
                                    src
                                }
                            }
                            else {
                                return item
                            }
                        })
                    })
                } else {
                    handleUpdate({
                        list: [...list, {
                            ...swiperListProps,
                            src
                        }]
                    })
                }

            }
        }} visible={visible} onClose={() => { setVisible(false) }}></UploadImage>
        <EditLink handleCallback={(e) => {
            const { index } = editVisible
            handleUpdate({
                list: list.map((item, o) => {
                    if (index === o) {
                        return {
                            ...item,
                            linkType: 'customize',
                            ...e
                        }
                    }
                    else {
                        return item
                    }
                })
            })
        }} visible={editVisible} onClose={() => { setEditVisible(false) }}></EditLink>
        <SelectRadioModel onOk={(e) => {
            if (e[0]) {
                const { index } = shopEdit
                handleUpdate({
                    list: list.map((item, o) => {
                        if (index === o) {
                            return {
                                ...item,
                                linkType: 'shop',
                                linkData: e[0]
                            }
                        }
                        else {
                            return item
                        }
                    })
                })
            }

        }} visible={shopEdit} onClose={() => setShopEdit(false)}></SelectRadioModel>
        <UploadImage handleImage={(e) => {
            if (e) {
                const { src } = e

                handleUpdate({
                    backgroundImage: src
                })
            }
        }} visible={bgVisible} onClose={() => { setBgVisible(false) }}></UploadImage>
    </Row>
}
export default DisplayAdvertising
